﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Web;
using System.Xml;
using System.Data;

namespace MediSIIM.BizComponent.HangZhouSYKT
{
    public class YiBaoJKDLL
    {
        [DllImport("YDJYInterface.dll", CharSet = CharSet.Ansi)]
        static extern int BUSINESS_HANDLE(
[MarshalAs(UnmanagedType.VBByRefStr)]ref string AKC803,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string AKB020,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string AKC020,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string MSGNO,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string MSGID,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string ORGMSGNO,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string ORGMSGID,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string OPERID,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string BATNO,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string INPUT,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string INDICATOR,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string WORKDATE,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string REFMSGID,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string OUTPUT,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string OUTPUTCOUNT,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string STATUS,
[MarshalAs(UnmanagedType.VBByRefStr)]ref string RESERVE);

        [DllImport("YDJYInterface.dll", CharSet = CharSet.Ansi)]
        public static extern int Get_Res([MarshalAs(UnmanagedType.VBByRefStr)]ref string Item_No, [MarshalAs(UnmanagedType.VBByRefStr)]ref string OUTPUT);

        public static ZIFUBL GetZFBL(int itemno)
        {
            try
            {
                string output = string.Empty.PadRight(1024);
                string ino = itemno.ToString();
                if (Get_Res(ref ino, ref output) >= 0)
                {
                    var xmldom = new XmlDocument();
                    xmldom.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + output.TrimEnd(' ', '\0'));
                    var xmlnode = xmldom.SelectSingleNode("/OUTPUT");
                    var zifubl = new ZIFUBL();
                    zifubl.AKC220 = xmlnode["AKC220"].InnerText;//处方流水号
                    zifubl.AKC228 = xmlnode["AKC228"].InnerText;//自理金额
                    zifubl.AKC253 = xmlnode["AKC253"].InnerText;//自费金额
                    zifubl.AKA069 = xmlnode["AKA069"].InnerText;//	自付比例
                    zifubl.AKC601 = xmlnode["AKC601"].InnerText;//	    限价
                    return zifubl;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static HangZhouSYKTradeParms YBCall(HangZhouSYKTradeParms parms)
        {
            string str = "<INPUT>";
            foreach (var item in parms.Items.Keys)
            {
                str += string.Format("<{0}>{1}</{0}>", item, parms[item]);
            }
            parms.INPUT = str + "</INPUT>";
            var retcode = YiBaoJKDLL.YBCall(
                                ref parms.AKC803,
                                ref parms.AKB020,
                                ref parms.AKC020,
                                ref parms.MSGNO,
                                ref parms.TradeNo,
                                ref parms.ORGMSGNO,
                                ref parms.ORGMSGID,
                                ref parms.OPERID,
                                ref parms.YWZQH,
                                ref parms.INPUT,
                                ref parms.INDICATOR,
                                ref parms.WORKDATE,
                                ref parms.REFMSGID,
                                ref parms.OUTPUT,
                                ref parms.OUTPUTCOUNT,
                                ref parms.STATUS,
                                ref parms.RESERVE);

            parms.WORKDATE = parms.WORKDATE.TrimEnd(' ', '\0');
            parms.REFMSGID = parms.REFMSGID.TrimEnd(' ', '\0');
            parms.OUTPUT = parms.OUTPUT.TrimEnd(' ', '\0');
            parms.OUTPUTCOUNT = parms.OUTPUT.TrimEnd(' ', '\0');
            parms.STATUS = parms.STATUS.TrimEnd(' ', '\0');
            parms.RESERVE = parms.RESERVE.TrimEnd(' ', '\0');
            if (parms.OUTPUTCOUNT == "1" || parms.OUTPUTCOUNT == "2")
            {
                parms.Succeed = retcode != -1;
            }
            else
            {

                if (parms.INDICATOR != "0" && string.IsNullOrEmpty(parms.STATUS))
                {
                    parms.Succeed = true;
                }
                else
                {
                    parms.Succeed = parms.STATUS == "0000";
                }
            }
            if (!parms.Succeed)
            {
                if (parms.RESERVE != "已经对帐")
                {
                    throw new Exception("医保中心:" + parms.RESERVE.TrimEnd(' ', '\0'));
                }
                else
                {
                    parms.Succeed = true;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(parms.OUTPUT))
                {
                    if (parms.MSGNO == "1601")
                    {
                        parms.OUTPUT = string.Format("<OUTPUT><FILENAME>{0}</FILENAME></OUTPUT>", parms.OUTPUT);
                    }
                    var xmldom = new XmlDocument();
                    xmldom.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + parms.OUTPUT);
                    var xmlnode = xmldom.SelectSingleNode("/OUTPUT");
                    if (xmlnode != null)
                    {
                        foreach (XmlNode node in xmlnode.ChildNodes)
                        {
                            parms.OutItems.Add(node.Name, node.InnerText);
                            parms.OutKeys.Add(node.Name);
                        }
                    }
                }
            }
            return parms;
        }

        public class ZIFUBL
        {
            /// <summary>
            /// 处方流水号
            /// </summary>
            public string AKC220 { get; set; }
            /// <summary>
            /// 自理金额
            /// </summary>
            public string AKC228 { get; set; }
            /// <summary>
            /// 自费金额
            /// </summary>
            public string AKC253 { get; set; }
            /// <summary>
            /// 自付比例
            /// </summary>
            public string AKA069 { get; set; }
            /// <summary>
            /// 限价
            /// </summary>
            public string AKC601 { get; set; }

        }

        public static int YBCall(
        ref string AKC803,
        ref string AKB020,
        ref string AKC020,
        ref string MSGNO,
        ref string MSGID,
        ref string ORGMSGNO,
        ref string ORGMSGID,
        ref string OPERID,
        ref string BATNO,
        ref string INPUT,
        ref string INDICATOR,
        ref string WORKDATE,
        ref string REFMSGID,
        ref string OUTPUT,
        ref string OUTPUTCOUNT,
        ref string STATUS,
        ref string RESERVE)
        {
            return BUSINESS_HANDLE(
                ref AKC803,
                ref AKB020,
                ref AKC020,
                ref MSGNO,
                ref MSGID,
                ref ORGMSGNO,
                ref ORGMSGID,
                ref OPERID,
                ref BATNO,
                ref INPUT,
                ref INDICATOR,
                ref WORKDATE,
                ref REFMSGID,
                ref OUTPUT,
                ref OUTPUTCOUNT,
                ref STATUS,
                ref RESERVE);
        }
    }
}
